use std::time::Duration;
use crate::common::types::R2D2Pool;
use super::app::AppConfig;

pub struct RedisPool;

impl RedisPool {
    pub async fn init(config: &AppConfig) -> R2D2Pool {
        let redis_url = format!(
            "redis://:{}@{}:{}/{}",
            config.redis_password, config.redis_host, config.redis_port, config.redis_db
        );

        log::info!("Redis URL: {}", redis_url);

        let client = redis::Client::open(redis_url).expect("Failed to connect to redis");

        r2d2::Pool::builder()
            .max_size(10)
            .connection_timeout(Duration::from_secs(30))
            .test_on_check_out(true)
            .min_idle(Some(5))
            .build(client)
            .expect("Failed to create redis pool")
    }
}
